package com.jykj.modbus.equipment.dao.guard;


import com.jykj.modbus.equipment.entity.guard.Equipment;
import com.jykj.modbus.equipment.entity.guard.EquipmentData;
import com.jykj.modbus.equipment.entity.guard.EquipmentMessage;
import com.jykj.modbus.system.runtime.hibernate.BaseDao;
import com.jykj.modbus.system.runtime.mvc.BaseLocal;
import com.jykj.util.StringUtils;
import com.jykj.util.TextUtil;
import org.hibernate.query.Query;

import javax.annotation.Resource;
import java.util.Date;

public class EquipmentDaoGuard {

    @Resource
    public BaseDao baseDao;

    //线程局部变量，用于存放当前用户信息等数据
    private static final ThreadLocal<BaseLocal> threadLocal = new ThreadLocal<BaseLocal>();

    /**
     * 得到当前线程（本地）数据对象
     * @return
     */
    public static BaseLocal getThreadLocal() {
        if(threadLocal.get() == null){
            return new BaseLocal();
        }
        else{
            return threadLocal.get();
        }
    }

    /**
     * 把数据存入当前线程（本地）对象
     * @param local 设置好共享值的对象
     */
    public static void setThreadLocal(BaseLocal local) {
        threadLocal.set(local);
    }

    /*-------------------------------------------------开始基础方法-------------------------------------------------*/
    /**查询单条**/
    public Equipment getEquipmentOne(Integer id) {
        Equipment ret = null;
        if(!TextUtil.isNotNull(id)){
            ret = new Equipment();
            //某些字段page默认值
        }else{
            ret = baseDao.getById(Equipment.class, id);
        }
        return ret;
    }


    /**保存设备信息单条，主要用于新增**/
    public void saveEquipment(Equipment obj) {
        Date now = new Date();
        //自动处理特殊默认值（特殊字段）
        obj.setUpdateTime(now);
        //进行新增保存
        baseDao.save(obj);
    }

    /**保存设备报文信息单条，主要用于新增**/
    public void saveEquipmentMessage(EquipmentMessage obj) {
        Date now = new Date();
        //自动处理特殊默认值（特殊字段）
        //进行新增保存
        baseDao.save(obj);
    }

    /**修改、更新方法**/
    public int updateEquipment(Equipment obj) {
        Date now = new Date();
        //得到当前用户ID
        String hql = "update Equipment set updateTime=:updateTime ";
        //设备识别码
        if (obj.getIdentificationCode() != null){
            hql += " ,identificationCode = :identificationCode  ";
        }
        //波特库
        if (obj.getBaudRate() != null){
            hql += " ,baudRate = :baudRate  ";
        }
        //串口号
        if (obj.getSerialPort() != null){
            hql += " ,serialPort = :serialPort  ";
        }
        //传感器类型
        if (obj.getSensorType() != null){
            hql += " ,sensorType = :sensorType  ";
        }
        hql += " where 1=1 and id = :id ";
        Query query = baseDao.createQuery(hql);
        query.setParameter("updateTime" , now);
        if (obj.getIdentificationCode()!=null){
            query.setParameter("identificationCode",obj.getIdentificationCode());
        }
        if (obj.getBaudRate()!=null){
            query.setParameter("baudRate",obj.getBaudRate());
        }
        if (obj.getSerialPort()!=null){
            query.setParameter("serialPort",obj.getSerialPort());
        }
        if (obj.getSensorType()!=null){
            query.setParameter("sensorType",obj.getSensorType());
        }
        query.setParameter("id",obj.getId());
        return query.executeUpdate();
    }

    /**通过设备识别码单条删除方法**/
    public void deleteEquipmentByid(Equipment obj) {
        String hql = "delete Equipment where 1=1 and identificationCode = :identificationCode";
        Query query = baseDao.createQuery(hql);
        query.setParameter("identificationCode",obj.getIdentificationCode());
        query.executeUpdate();
    }

}
